home *** CD-ROM | disk | FTP | other *** search
- /*______________________________________________________________________
-
- Sample - Disinfectant Source Code Sample Program.
-
- Version 2.0b1.
-
- John Norstad
- Academic Computing and Network Services
- Northwestern University
- 2129 Sheridan Road
- Evanston, IL 60208
-
- Bitnet: jln@nuacc
- Internet: jln@acns.nwu.edu
- AppleLink: a0173
- CompuServe: 76666,573
-
- Copyright © 1988, 1989, 1990 Northwestern University. Permission is
- granted to use this code in your own projects, provided you give credit
- to both John Norstad and Northwestern University in your about box or
- document.
- _____________________________________________________________________*/
-
-
- /*______________________________________________________________________
-
- prog.c - Disinfectant Main Module.
-
- This is the main module for Disinfectant. It contains the basic
- main event loop processing.
- _____________________________________________________________________*/
-
-
- #pragma load "precompile"
- #include "utl.h"
- #include "rez.h"
- #include "glob.h"
- #include "wstm.h"
- #include "abou.h"
- #include "pref.h"
- #include "help.h"
- #include "main.h"
- #include "misc.h"
- #include "vscn.h"
- #include "init.h"
- #include "prog.h"
-
- #pragma segment prog
-
- extern void _DataInit();
-
- /*_____________________________________________________________________
-
- Global Variables.
- _____________________________________________________________________*/
-
-
- static WindowPtr Front; /* ptr to front window */
- static WindowObject *FrontObj; /* ptr to front window object */
- static WindKind FrontKind; /* front window kind */
-
- /*_____________________________________________________________________
-
- Command - Process a Command.
-
- Entry: mResult = 16/menu number, 16/item number.
- _____________________________________________________________________*/
-
-
- static void Command (long mResult)
-
- {
- short theMenu; /* menu number */
- short theItem; /* item number */
- Str255 daName; /* desk accessory name */
- GrafPtr savePort; /* saved grafport */
-
- /* Extact the item and menu numbers. */
-
- theItem = mResult & 0xffff;
- theMenu = (mResult >> 16) & 0xffff;
-
- /* Check for and process help mode. */
-
- if (HelpMode) {
- if (theMenu)
- help_Open(tagCmdBase + tagCmdMult*(theMenu-appleMID) + theItem);
- HiliteMenu(0);
- return;
- };
-
- switch (theMenu) {
-
- /* Process the command. */
-
- case appleMID:
-
- if (theItem == aboutCommand) {
- abou_Open();
- } else if (theItem == helpCommand) {
- help_Open(0);
- } else {
- GetItem(GetMHandle(appleMID), theItem, daName);
- GetPort(&savePort);
- (void) OpenDeskAcc(daName);
- SetPort(savePort);
- };
- break;
-
- case fileMID:
-
- switch (theItem) {
- case closeCommand:
- if (FrontKind == daWind) {
- CloseDeskAcc(((WindowPeek)Front)->windowKind);
- } else {
- if (FrontObj->close) (*FrontObj->close)();
- };
- break;
- case saveAsCommand:
- if (FrontObj->save) (*FrontObj->save)();
- break;
- case pageSetupCommand:
- if (FrontObj->pageSetup) {
- PrOpen();
- (*FrontObj->pageSetup)();
- PrClose();
- };
- break;
- case printCommand:
- if (FrontObj->print) {
- PrOpen();
- misc_PrintError((*FrontObj->print)(false));
- PrClose();
- };
- break;
- case printOneCommand:
- if (FrontObj->print) {
- PrOpen();
- misc_PrintError((*FrontObj->print)(true));
- PrClose();
- };
- break;
- case prefsCommand:
- pref_Open();
- break;
- case quitCommand:
- Done = true;
- break;
- default:
- break;
- }
- break;
-
- case editMID:
- if (!SystemEdit(theItem-1) && FrontObj->edit)
- (*FrontObj->edit)(theItem-1);
- break;
-
- case scanMID:
- case disinfectMID:
-
- main_DoScan(theMenu, theItem);
- break;
-
- case protectMID:
- switch (theItem) {
- case installCommand:
- misc_InstallINIT();
- break;
- case extractCommand:
- misc_ExtractINIT();
- break;
- };
- break;
-
- default:
- break;
-
- }; /* switch */
-
- /* Turn off the menu hiliting and return. */
-
- HiliteMenu(0);
- }
-
- /*______________________________________________________________________
-
- Adjust - Adjust Menus.
- _____________________________________________________________________*/
-
-
- static void Adjust (void)
-
- {
- static WindKind oldFrontKind = mainWind;
- /* previous front window type */
- static Boolean oldScanning = false; /* previous scanning context */
- static Boolean oldHelpMode = false; /* previous help mode */
- MenuHandle fileM; /* handle to file menu */
- MenuHandle editM; /* handle to edit menu */
- MenuHandle scanM; /* handle to scan menu */
- MenuHandle disinfectM; /* handle to disinfect menu */
-
- fileM = GetMHandle(fileMID);
- editM = GetMHandle(editMID);
- scanM = GetMHandle(scanMID);
- disinfectM = GetMHandle(disinfectMID);
-
- FrontKind = misc_GetWindKind(FrontWindow());
-
- if (HelpMode) {
- if (oldHelpMode) return;
- EnableItem(fileM, closeCommand);
- EnableItem(fileM, saveAsCommand);
- EnableItem(fileM, pageSetupCommand);
- EnableItem(fileM, printCommand);
- EnableItem(fileM, printOneCommand);
- EnableItem(fileM, quitCommand);
- EnableItem(editM, undoCommand);
- EnableItem(editM, cutCommand);
- EnableItem(editM, copyCommand);
- EnableItem(editM, pasteCommand);
- EnableItem(editM, clearCommand);
- EnableItem(editM, 0);
- EnableItem(scanM, 0);
- EnableItem(disinfectM, 0);
- } else {
- if (oldFrontKind == FrontKind && oldScanning == Scanning &&
- oldHelpMode == HelpMode) return;
- if (FrontKind == daWind) {
- EnableItem(fileM, closeCommand);
- DisableItem(fileM, saveAsCommand);
- DisableItem(fileM, pageSetupCommand);
- DisableItem(fileM, printCommand);
- DisableItem(fileM, printOneCommand);
- if (Scanning) {
- DisableItem(fileM, quitCommand);
- DisableItem(scanM, 0);
- DisableItem(disinfectM, 0);
- } else {
- EnableItem(fileM, quitCommand);
- EnableItem(scanM, 0);
- EnableItem(disinfectM, 0);
- };
- EnableItem(fileM, 0);
- EnableItem(editM, undoCommand);
- EnableItem(editM, cutCommand);
- EnableItem(editM, copyCommand);
- EnableItem(editM, pasteCommand);
- EnableItem(editM, clearCommand);
- EnableItem(editM, 0);
- } else{
- if (FrontObj->adjust) (*FrontObj->adjust)();
- };
- };
- DrawMenuBar();
- oldFrontKind = FrontKind;
- oldScanning = Scanning;
- oldHelpMode = HelpMode;
- }
-
- /*_____________________________________________________________________
-
- prog_Event - Fetch and Handle the Next Event.
-
- This routine should be called to do all non-modal event processing.
- It handles suspend, resume, and update events properly. It also
- keeps the initial integrity checksum going.
- _____________________________________________________________________*/
-
-
- void prog_Event (void)
-
- {
- EventRecord event; /* event */
- short mask; /* event mask */
- WindowPtr whichWindow; /* ptr to window clicked in */
- WindowObject *whichObj; /* ptr to object for window clicked in */
- short partCode; /* window part code */
- short key; /* ascii code of key pressed */
- unsigned long newSize; /* new window size */
- long sleep; /* sleep time */
- Boolean eventAvail; /* WaitNextEvent function result */
- Point where; /* location of mouse down */
- GrafPtr savedPort; /* saved grafport */
- short item; /* dialog item */
-
- /* Save the current grafport. */
-
- GetPort(&savedPort);
-
- /* Get the front window and a pointer to its object. */
-
- Front = FrontWindow();
- FrontObj = (WindowObject*)((WindowPeek)Front)->refCon;
-
- /* Adjust menus. */
-
- Adjust();
-
- /* Take care of any periodic tasks for all open windows. */
-
- whichWindow = FrontWindow();
- while (whichWindow) {
- if (!utl_IsDAWindow(whichWindow)) {
- whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
- if (whichObj->periodic) {
- SetPort(whichWindow);
- (*whichObj->periodic)();
- };
- };
- whichWindow = (WindowPtr)((WindowPeek)whichWindow)->nextWindow;
- };
- SetPort(savedPort);
-
- /* Initialize the event mask and the sleep time. */
-
- mask = everyEvent;
- if ((Scanning && !FloppyWait) || !InForeground) mask ^= diskMask;
- sleep = (Scanning || FrontKind == prefWind ||
- abou_IsOpen()) ? 0 : LongSleep;
-
- /* Get the next event, if any. */
-
- eventAvail = utl_WaitNextEvent(mask, &event, sleep, nil);
-
- /* Check for and process dialog event. */
-
- if (IsDialogEvent(&event) && event.what != osEvt &&
- event.what != diskEvt &&
- (event.what != keyDown || !(event.modifiers&cmdKey)) &&
- (event.what != mouseDown || !HelpMode)) {
- if (event.what != keyDown || !FrontObj->dialogPre ||
- (*FrontObj->dialogPre)(event.message & charCodeMask)) {
- if (DialogSelect(&event, &whichWindow, &item)) {
- whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
- if (whichObj->dialogPost) {
- SetPort(whichWindow);
- (*whichObj->dialogPost)(item);
- };
- };
- };
- SetPort(savedPort);
- return;
- };
-
- /* Return if there's no event to handle. */
-
- if (!eventAvail) return;
-
- /* Process the event. */
-
- switch (event.what) {
-
- case mouseDown:
-
- partCode = FindWindow(event.where, &whichWindow);
- if (whichWindow && !utl_IsDAWindow(whichWindow))
- whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
-
- switch (partCode) {
-
- case inSysWindow:
-
- SystemClick(&event, whichWindow);
- break;
-
- case inMenuBar:
-
- MenuPick = true;
- Command(MenuSelect(event.where));
- break;
-
- case inGoAway:
-
- if (TrackGoAway(whichWindow, event.where))
- if (whichObj->close) (*whichObj->close)();
- break;
-
- case inDrag:
-
- DragWindow(whichWindow, event.where, &DragRect);
- whichObj->moved = true;
- break;
-
- case inGrow:
-
- SetPort(whichWindow);
- newSize = GrowWindow(whichWindow, event.where,
- &whichObj->sizeRect);
- if (newSize && whichObj->grow)
- (*whichObj->grow)(newSize >> 16, newSize & 0xffff);
- whichObj->moved = true;
- break;
-
- case inZoomIn:
- case inZoomOut:
-
- if (TrackBox(whichWindow, event.where, partCode)) {
- SetPort(whichWindow);
- if (partCode == inZoomOut) wstm_ComputeStd(whichWindow);
- EraseRect(&whichWindow->portRect);
- ZoomWindow(whichWindow, partCode, false);
- if (whichObj->zoom) (*whichObj->zoom)();
- whichObj->moved = true;
- };
- break;
-
- case inContent:
-
- if (whichWindow != FrontWindow()) {
- SelectWindow(whichWindow);
- } else {
- SetPort(whichWindow);
- where = event.where;
- GlobalToLocal(&where);
- if (HelpMode) {
- if (whichObj->help) (*whichObj->help)(where);
- } else {
- if (whichObj->click)
- (*whichObj->click)(where, event.modifiers);
- };
- };
- break;
-
- };
-
- misc_SetCursor();
-
- break;
-
- case keyDown:
- case autoKey:
-
- SetPort(Front);
- key = event.message & charCodeMask;
- if ((event.modifiers & cmdKey) && key != upArrow &&
- key != downArrow) {
- if (key == '?' || key == '/') {
- HelpMode = true;
- misc_SetCursor();
- misc_DisableBB();
- } else if (key == '.' && HelpMode) {
- HelpMode = false;
- misc_SetCursor();
- } else if (key == '.' && Scanning) {
- Scanning = FloppyWait = false;
- Canceled = true;
- misc_SetCursor();
- } else {
- MenuPick = false;
- Command(MenuKey(event.message & charCodeMask));
- };
- } else {
- if (FrontObj->key) (*FrontObj->key)(key, event.modifiers);
- };
- break;
-
- case updateEvt:
-
- whichWindow = (WindowPtr)event.message;
- whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
- SetPort(whichWindow);
- BeginUpdate(whichWindow);
- EraseRect(&whichWindow->portRect);
- if (whichObj->update) (*whichObj->update)();
- EndUpdate(whichWindow);
- break;
-
- case activateEvt:
-
- whichWindow = (WindowPtr)event.message;
- whichObj = (WindowObject*)((WindowPeek)whichWindow)->refCon;
- SetPort(whichWindow);
- if (event.modifiers & activeFlag) {
- if (whichObj->activate) (*whichObj->activate)();
- } else {
- if (whichObj->deactivate) (*whichObj->deactivate)();
- };
- break;
-
- case diskEvt:
-
- main_Disk(event.message);
- break;
-
- case osEvt:
-
- if (((event.message >> 24) & 0xff) != suspendResumeMessage) break;
- InForeground = event.message & 1;
- if (FrontKind != daWind) {
- SetPort(Front);
- if (InForeground) {
- if (FrontObj->activate) (*FrontObj->activate)();
- } else {
- if (FrontObj->deactivate) (*FrontObj->deactivate)();
- };
- };
- if (InForeground) {
- misc_SetCursor();
- if (Notified) {
- Notified = false;
- if (Prefs.notifOption != notifAlert)
- NMRemove((QElemPtr)&NotifRec);
- };
- } else {
- InitCursor();
- };
- break;
-
- };
-
- /* Restore the saved grafPort. */
-
- SetPort(savedPort);
-
- };
-
- /*______________________________________________________________________
-
- Main - The Main Program.
- _____________________________________________________________________*/
-
-
- void main(void)
-
- {
- Str255 msg; /* message to plug in restart alert */
-
- /* Initialize. */
-
- UnloadSeg(_DataInit);
- init_InitMem();
- init_Initialize();
- UnloadSeg(init_Initialize);
-
- /* If scanning station, start scan. */
-
- if (Prefs.scanningStation) vscn_DoScan(autoScan, Prefs.scanningStationOp);
-
- /* Main event loop. */
-
- while (true) {
-
- prog_Event();
-
- if (Done) {
- if (RepInfected) {
- switch (utl_StopAlert(saveRepID1, nil, 3)) {
- case 1: /* Yes */
- Done = main_Save();
- break;
- case 2: /* No */
- break;
- case 3: /* Cancel */
- Done = false;
- break;
- };
- };
- if (Done && SysInfect) {
- GetIndString(msg, strListID,
- utl_VolIsMFS(utl_GetSysVol()) ? oldRestartStr : restartStr);
- ParamText(msg, nil, nil, nil);
- if (SysHasShutDown) {
- switch (utl_StopAlert(restartID, nil, 2)) {
- case 1: /* Restart */
- misc_WritePref();
- ShutDwnStart();
- break;
- case 2: /* Cancel */
- Done = false;
- break;
- case 3: /* Quit */
- break;
- };
- } else {
- switch (utl_StopAlert(oldRestartID, nil, 1)) {
- case 1: /* Cancel */
- Done = false;
- break;
- case 2: /* Quit */
- break;
- };
- }
- };
- if (Done) break;
- };
-
- };
-
- /* Terminate. */
-
- misc_WritePref();
- }
-